package ${content.controller.classPackage!}<#if content.base.module?exists>.${content.base.module}</#if>;

import com.google.common.collect.Lists;
import ${content.base.basePacket!}.common.constants.sys.GatewayPath;
import ${content.base.basePacket!}.common.constants.sys.Pages;
import ${content.base.basePacket!}.common.domain.user.UserAddress;
import ${content.base.basePacket!}.common.dto.page.DataPage;
import ${content.base.basePacket!}.common.util.ConvertUtil;
import ${content.base.basePacket!}.database.dao.base.condition.ConditionGroup;
import ${content.base.basePacket!}.pm.service<#if content.base.module?exists>.${content.base.module}</#if>.${content.entity.className!}Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * ${content.controller.className!}的Controller接口
 *
 * @author  : ${content.base.author!}
 * @version : ${content.base.version!}
 * @date    : ${content.base.createTime!}
 */
@RestController
@RequestMapping(GatewayPath.API_PM)
public class ${content.controller.className!} {

  @Autowired
  private ${content.service.className!} ${content.service.className?uncap_first};

 /**
  * 分页
  * @param params -
  * <p> pageNum        - Nullable   - Int - 当前页
  * <p> pageSize       - Nullable   - Int - 每页记录数
  *
  * @return DataPage
  */
  @GetMapping(value = "<#if content.base.module?exists>/${content.base.module}</#if>/${content.entity.className?uncap_first}/page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity page(@RequestParam Map<String, String> params) {
    ConditionGroup<${content.entity.className!}> conditionGroup = ConditionGroup.build();
    // 条件: e.g. conditionGroup.addCondition("${content.entity.primaryKey!}", ConditionEnum.OPERATE_EQUAL,params.get("id"))
    return new ResponseEntity<>(DataPage.from(${content.service.className?uncap_first}.findAll(
    Pages.Helper.pageable(params, new Sort(Sort.Direction.ASC, "${content.entity.primaryKey!}")))), HttpStatus.OK);
  }

 /**
  * 根据ID查询
  * @param id -
  * @return ${content.entity.className?uncap_first}
  */
  @GetMapping(value = "<#if content.base.module?exists>/${content.base.module}</#if>/${content.entity.className?uncap_first}/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity findById(@PathVariable String id) {
    return new ResponseEntity<>(${content.service.className?uncap_first}.findById(id), HttpStatus.OK);
  }

 /**
  * 新增
  * @param params -
  * @return ${content.entity.className?uncap_first}
  */
  @PostMapping(value = "<#if content.base.module?exists>/${content.base.module}</#if>/${content.entity.className?uncap_first}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity save(@RequestBody Map<String, String> params) {
    ${content.entity.className} ${content.entity.className?uncap_first} = ConvertUtil.populate(new ${content.entity.className}(), params);
    return new ResponseEntity<>(${content.service.className?uncap_first}.saveOrUpdate(${content.entity.className?uncap_first}), HttpStatus.CREATED);
  }

 /**
  * 修改
  * @param params -
  * @return ${content.entity.className?uncap_first}
  */
  @PutMapping(value = "<#if content.base.module?exists>/${content.base.module}</#if>/${content.entity.className?uncap_first}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity update(@RequestBody Map<String, String> params) {
    ${content.entity.className} ${content.entity.className?uncap_first} = ConvertUtil.populate(${content.service.className?uncap_first}.findById(params.get("id")), params);
    return new ResponseEntity<>(${content.service.className?uncap_first}.saveOrUpdate(${content.entity.className?uncap_first}), HttpStatus.ACCEPTED);
  }

 /**
  * 删除
  * @param ids -
  * @return -
  */
  @DeleteMapping(value = "<#if content.base.module?exists>/${content.base.module}</#if>/${content.entity.className?uncap_first}/{ids}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity delete(@PathVariable String ids) {
    String[] idsList = StringUtils.split(ids, ",");
    ${content.service.className?uncap_first}.delete(Lists.newArrayList(idsList));
    return new ResponseEntity<>(HttpStatus.ACCEPTED);
  }

 /**
  * 删除
  * @param params -
  * @return -
  */
  @DeleteMapping(value = "<#if content.base.module?exists>/${content.base.module}</#if>/${content.entity.className?uncap_first}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity delete(@RequestBody Map<String, String> params) {
    String[] idsList = StringUtils.split(params.get("ids"), ",");
    ${content.service.className?uncap_first}.delete(Lists.newArrayList(idsList));
    return new ResponseEntity<>(HttpStatus.ACCEPTED);
  }
}
